<?php
class User_RegisterController extends Zend_Controller_Action
{

    public function init()
    {
		$this->_helper->layout()->setLayout('register_login');
		$this->_SESSION = new Zend_Session_Namespace();
        /* Initialize action controller here */
		$this->_SESSION=new Zend_Session_Namespace();
    }

    public function indexAction()
    {
		session_start();
        if(isset($this->_SESSION->iduser)){
			header("location: http://localhost/hoidap/Source/user");
			//echo "<script>alert('Bạn cần đăng xuất trước khi thực hiện đăng ký');</script>";
		}
		$this->view->imgcaptcha = ROOT.'application/modules/captcha/captcha.php';
		//$this->view->captcha_session = $_SESSION['macaptcha'];
    }
	

	
	public function registerxulyAction()
	{
		session_start();
		$this->_helper->viewRenderer->setNoRender(true);
		$this->_helper->layout->disableLayout();
		
		if($_SESSION['macaptcha'] != $_POST['captcha']){
			$flag = -1;
		}else{
			$user = new User_Model_DbTable_Users();
			$firstname = $_POST['firstname'];
			$lastname = $_POST['lastname'];
			$email = $_POST['email'];
			$password = $_POST['password'];
			$sex = $_POST['sex'];
			
			$avata = "";
			$phone = "";
			$address = "";
			
			$checkemail = $user -> kiemtraemailtontai($email);
			if($checkemail != 0){
				$flag = 0;
			}else{
				$dt = getdate();
				$time = $dt['year'].'-'.$dt['mon'].'-'.$dt['mday'].' '.($dt['hours']+5).':'.$dt['minutes'].':'.$dt['seconds'];
				
				$idnewuser = $user -> insertuser($firstname, $lastname, $email, $password, $avata, $sex, $phone, $address);
				
				$this->_SESSION->iduser=$idnewuser;
				$this->_SESSION->firstname=$firstname;
				$this->_SESSION->lastname=$lastname;

				$flag = 1;
			}
		}
		$kq=array('ketqua'=>$flag);
		echo Zend_Json::encode($kq);
    }


}

